package com.example.demo.serviceimp;

import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImp implements UserService {

    @Resource
    private UserMapper ump;

    @Resource
    JavaMailSenderImpl mailSender;

    /**
     * 从配置文件中获取发件人
     */
    @Value("${spring.mail.username}")
    private String sender;

    /**
     * 注册
     * */
    @Override
    public int reg(String user_name, String user_password, String user_email,String user_code){
        return ump.reg(user_name,user_password,user_email,user_code);
    }
    @Override
    public User userid(String user_email) {
        return ump.userid(user_email);
    }

    //发送邮件
    @Override
    public int emiles(String user_email, String user_code) {
        SimpleMailMessage message = new SimpleMailMessage();
        System.out.println(user_code);
        message.setSubject("激活码");	//设置邮件标题
        message.setText("尊敬的用户,您好:\n"
                + "\n本次请求的激活码为:" + user_code + ",（请勿泄露和忘记此激活码，用于后续找回密码）\n"
                + "\n如非本人操作，请忽略该邮件。\n(这是一封自动发送的邮件，请不要直接回复）");	//设置邮件正文
        message.setTo(user_email);	//设置收件人
        message.setFrom(sender);	//设置发件人
        mailSender.send(message);	//发送邮件

        return ump.emiles(user_email,user_code);
    }

    @Override
    public int role(Integer user_id) {
        return ump.role(user_id);
    }



    //查询邮箱是否被注册(前端输入框提示时调用)
    @Override
    public User select_email(String user_email){
        return ump.select_email(user_email);

    }

    /**
     * 登录
     * */
    @Override
    public User log(String user_email, String user_password){
        return ump.log(user_email,user_password);
    }

    /**
     * 查看个人信息
     * */
    @Override
    public User toview(String user_email){
        return ump.toview(user_email);
    }

    /**
     * 查询所有学生
     * */
    @Override
    public List selstu(Integer page,Integer limit) {
        page=(page-1)*limit;
        return ump.selstu(page, limit);
    }

    @Override
    public int selusernums() {
        return ump.selusernums();
    }


    /**
     * 更改用户状态
     * */
    @Override
    public int update(Integer user_status,String user_email) {
        return ump.update(user_status,user_email);
    }

    /**
     * 用户自行更改密码
     * */
    @Override
    public int uppwd(String new_user_password,String user_password, String user_email) {
        return ump.uppwd(new_user_password,user_password,user_email);
    }

    /**
     * 管理员重置用户密码
     * */
    @Override
    public int admuppwd(String user_email) {
        return ump.admuppwd(user_email);
    }

    @Override
    public List<Role> selrole() {
        return ump.selrole();
    }

    @Override
    public Role seluserrole(String user_email) {
        return ump.seluserrole(user_email);
    }

    @Override
    public List<HashMap> selusermohu(String user_name, String user_email) {
        return ump.selusermohu(user_name, user_email);
    }


}
